//
//  SBS_NoRotation.m
//  SBS_NoRotation
//
//  Created by crash-x on 30.12.08.
//


#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>



#define LOG_NOTHING 0
#define LOG_ERROR 1
#define LOG_WARNING 2
#define LOG_INFO 3
#define LOG_DEBUG 4
#define MAX_LOG_LEVEL 4
#define CONFIG_PATH "/var/mobile/Library/SBSettings/Toggles/NoRotation/NoRotation.plist"
#define APP_NAME "SBS_NoRotation"

int myLogLevel = LOG_DEBUG;
BOOL killSafari = NO;

void myLog(int logLevel, NSString * formatString, ...) {
	NSString * logString;
	va_list argumentList;
	char *logLevelStrings[] = {"None", "Error", "Warning", "Info", "Debug"};
	
	if(myLogLevel != LOG_NOTHING && logLevel > MAX_LOG_LEVEL)
		return;
	
	if(logLevel > MAX_LOG_LEVEL || logLevel == LOG_NOTHING)
		return;
	
	if(logLevel > myLogLevel)
		return;
	
	va_start (argumentList, formatString);
	logString = [[NSString alloc] initWithFormat:formatString arguments:argumentList];
	va_end (argumentList);
	
	NSLog(@"%s:%s: %@", APP_NAME, logLevelStrings[logLevel], logString);
	
	[logString release];
}



BOOL isCapable() {
	return YES;
}

BOOL isEnabled() {
	NSDictionary *config = [[NSDictionary alloc] initWithContentsOfFile:@CONFIG_PATH];
	int enabled = 0;
	
	if (config != nil) {	
		NSString *tmp = [config objectForKey:@"enabled"];
		if(tmp != nil) {
			enabled = [tmp intValue];
		}
		[tmp release];
	    [config release];
	} else {
        myLog(LOG_ERROR, @"Wasnt able to open config file for reading in isEnabled!");
		return NO;
    }
	
	
	if(enabled){
		return YES;
	} else {
		return NO;
	}
}

// Disabled since NoRotation is only toggled by SBSettings
#if 0
BOOL getStateFast() {
	return isEnabled();
}
#endif


// Thanks to BigBoss for writing that one
void KillApp(NSString *appSuffix) {
	Class SBApplicationController = objc_getClass("SBApplicationController");
	id appController = [SBApplicationController sharedInstance];
	NSArray* apps = [appController allApplications];
	int i;
	
	if(apps != nil) {	
		for(i = 0; i < [apps count]; i++) { 
			id App = [apps objectAtIndex:i];
			//[App pid];
			
			// Pid = -1 means app is not running.
			if([App pid] != -1 && [[App bundleIdentifier] hasSuffix:appSuffix]) {
				[App kill];
			}
		}
	}
}



void setState(BOOL Enabled) {
	NSMutableDictionary *config = [[NSMutableDictionary alloc] initWithContentsOfFile:@CONFIG_PATH];
	BOOL iPodAlwaysEnabled = NO;
	BOOL safariAlwaysEnabled = NO;
	BOOL iPodAlwaysDisabled = NO;
	BOOL safariAlwaysDisabled = NO;
	
	
	if (config != nil) {	
		if(Enabled == YES)
			[config setObject:[NSNumber numberWithInt:1] forKey:@"enabled"];
		else
			[config setObject:[NSNumber numberWithInt:0] forKey:@"enabled"];			
		
	    if([config writeToFile: @CONFIG_PATH atomically: YES] == NO) {
            myLog(LOG_ERROR, @"Wasnt able to write config!");
        }
		
		
		NSString *tmp = [config objectForKey:@"iPodAlwaysEnabled"];
		if(tmp != nil) {
			if([tmp intValue] == 1)
				iPodAlwaysEnabled = YES;
		} 	
		tmp = [config objectForKey:@"SafariAlwaysEnabled"];
		if(tmp != nil) {
			if([tmp intValue] == 1)
				safariAlwaysEnabled = YES;
		} 	
		tmp = [config objectForKey:@"iPodAlwaysDisabled"];
		if(tmp != nil) {
			if([tmp intValue] == 1)
				iPodAlwaysDisabled = YES;
		} 	
		tmp = [config objectForKey:@"SafariAlwaysDisabled"];
		if(tmp != nil) {
			if([tmp intValue] == 1)
				safariAlwaysDisabled = YES;
		} 	
		
		myLog(LOG_DEBUG, @"iPodAlwaysEnabled: %d", iPodAlwaysEnabled);
		myLog(LOG_DEBUG, @"safariAlwaysEnabeld: %d", safariAlwaysEnabled);
		myLog(LOG_DEBUG, @"iPodAlwaysDisabled: %d", iPodAlwaysDisabled);
		myLog(LOG_DEBUG, @"safariAlwaysDisabled: %d", safariAlwaysDisabled);

		
		if(safariAlwaysEnabled != YES && safariAlwaysDisabled != YES)
			KillApp(@"mobilesafari");
		if(iPodAlwaysEnabled != YES && iPodAlwaysDisabled != YES)
			KillApp(@"mobileipod");
		
	    [config release];
	} else {
        myLog(LOG_ERROR, @"Wasnt able to open config file in setState!");
    }
}


float getDelayTime() {
	return 1.0f;
}


__attribute__((constructor))
static void SBS_UAFakerInitializer() {
	NSDictionary *config = [[NSDictionary alloc] initWithContentsOfFile:@CONFIG_PATH];
	
	if (config != nil) {	
		NSString *tmp = [config objectForKey:@"logLevel"];
		if(tmp != nil) {
			myLogLevel = [tmp intValue];
			if(myLogLevel < 0 || myLogLevel > MAX_LOG_LEVEL)
				myLogLevel = LOG_NOTHING;
		} else {
			myLogLevel = LOG_NOTHING;
		}
	}
	
    myLog(LOG_DEBUG, @"loaded!");
}
